﻿@*
    // Copyright (c) Alex Reich.
    // Licensed under the CC BY 4.0 License.
*@
@page "/DemoEF"
@implements IDisposable
@using RulesEngine.Models
@using RulesEngineEditor.Models
@using Microsoft.EntityFrameworkCore
@using RulesEngineEditor.Data

@inject IDbContextFactory<RulesEngineEditorDbContext> DbFactory

 Last Saved: @lastSaved
<RulesEngineEditorPage EditorRulesEngine="re" @bind-WorkflowDatas="WorkflowDatas" MenuButtons="menuButtons" WorkflowsSaved="Save" />

@code {
    private RulesEngineEditorDbContext db;
    private RulesEngine.RulesEngine re;

    private List<WorkflowData> _workflowDatas;
    private List<WorkflowData> WorkflowDatas
    {
        get { return _workflowDatas; }
        set
        {
            _workflowDatas = value;

            //add any missing workflows into dbcontext
            foreach (var wf in _workflowDatas)
            {
                if (!db.Workflows.Any(w => w.Id == wf.Id))
                    db.Workflows.Add(wf);
            }

            //remove any missing workflows into dbcontext
            foreach (var wf in db.Workflows)
            {
                if (!_workflowDatas.Any(w => w.Id == wf.Id))
                    db.Workflows.Remove(wf);
            }

            try
            {
                if (db.ChangeTracker.HasChanges())
                {
                    db.SaveChanges(true);

                    RefreshDB();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }

    DateTime? lastSaved;

    enum FooEnum
    {
        bar = 123
    }

    public void Dispose()
    {
        db?.Dispose();
    }

    void RefreshDB()
    {
        WorkflowDatas = db.Workflows.Include(i => i.GlobalParams.OrderBy(o => o.Seq)).Include(i => i.Rules.OrderBy(o => o.Seq)).ThenInclude(i => i.Rules.OrderBy(o => o.Seq)).ThenInclude(i => i.LocalParams.OrderBy(o => o.Seq)).ToList();
    }

    void Save(Workflow[] wfs)
    {
        lastSaved = DateTime.Now;
        db.Workflows.UpdateRange(WorkflowDatas.ToList());
        db.SaveChanges();
    }

    private List<MenuButton> menuButtons = new List<MenuButton>();

    protected override void OnInitialized()
    {
        menuButtons.Add(new MenuButton("NewWorkflows", false));
        menuButtons.Add(new MenuButton("SaveWorkflow", true));

        var settings = new ReSettings
        {
            CustomTypes = new Type[] { typeof(FooEnum) }
        };
        re = new RulesEngine.RulesEngine(new string[0], settings);

        db = DbFactory.CreateDbContext();
        db.Database.EnsureCreated();

        RefreshDB();
    }
}
